/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.search.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.dao.search.KeywordsRecommendDao;
import org.grayrabbit.cms.entity.KeywordsRecommend;
import org.grayrabbit.cms.vo.search.KeywordsRecommendBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>KeywordsRecommend</b> dao implementation 





 * @version 1.0，创建时间：2011-10-26 13:42:14 
 */
@Repository
public class KeywordsRecommendDaoImpl extends GenericHibernateDao<KeywordsRecommend, String>
		implements KeywordsRecommendDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From KeywordsRecommend as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see KeywordsRecommendDao#assembly(KeywordsRecommendBean)
	 */
	public QueryCondition assembly(KeywordsRecommendBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see KeywordsRecommendDao#assemblyCount(KeywordsRecommendBean)
	 */
	public QueryCondition assemblyCount(KeywordsRecommendBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see KeywordsRecommendDao#count(KeywordsRecommendBean)
	 */
	public int count(KeywordsRecommendBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see KeywordsRecommendDao#assemblyNameQuery(KeywordsRecommendBean)
	 */
	public QueryCondition assermblyNameQuery(KeywordsRecommendBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see KeywordsRecommendDao#assemblyNameQueryCount(KeywordsRecommendBean)
	 */
	public QueryCondition assemblyNameQueryCount(KeywordsRecommendBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see KeywordsRecommendDao#findByPage(Page, KeywordsRecommendBean)
	 */
	public List<KeywordsRecommend> findByPage(Page<KeywordsRecommend> page,
			KeywordsRecommendBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(KeywordsRecommendBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getRecommendName())){
				qc.add("And obj.recommendName like ? ", bean.getRecommendName()+"%");
			}
			if(StringUtils.isNotBlank(bean.getRecommendurl())){
				qc.add("And obj.recommendurl like ? ", bean.getRecommendurl()+"%");
			}
            if(bean.getRecommendtype() != null){
				qc.add("And obj.recommendtype = ? ", bean.getRecommendtype());
			}
            if(bean.getKeywords() != null&&StringUtils.isNotBlank(bean.getKeywords().getKeywordsId())){
				qc.add("And obj.keywords.id = ? ", bean.getKeywords().getKeywordsId());
			}

		}
		qc.appenOrderBy(" obj.recommendtype desc");
	}
	

	public boolean isExistUrl(String id, String url) {
		if(StringUtils.isBlank(url))
			return false;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add("And obj.recommendurl = ? ", url);
		List<KeywordsRecommend> list = findByQc(qc);
		if(list.isEmpty())
			return false;
		if(StringUtils.isBlank(id))
			return true;
		KeywordsRecommend kr = list.get(0);
		if(kr.getKwrecommendId().equals(id))
			return false;
		return false;
	}

	
	public void deleteByKewordsId(String keywordsid) {
		QueryCondition qc = new QueryCondition("delete "+MAIN_HQL);
		qc.add("And obj.keywords.id = ? ", keywordsid);
		this.bulkUpdate(qc);
	}

	public List<KeywordsRecommend> list(String keywordsId) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add("And obj.keywords.id = ? ", keywordsId);
		qc.appenOrderBy(" obj.recommendtype desc");
		return findByQc(qc);
	}


}